<?php declare(strict_types=1);
/*
 * Copyright (c) 2023 cclilshy
 * Contact Information:
 * Email: jingnigg@gmail.com
 * Website: https://cc.cloudtay.com/
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * 版权所有 (c) 2023 cclilshy
 *
 * 特此免费授予任何获得本软件及相关文档文件（“软件”）副本的人，不受限制地处理
 * 本软件，包括但不限于使用、复制、修改、合并、出版、发行、再许可和/或销售
 * 软件副本的权利，并允许向其提供本软件的人做出上述行为，但须符合以下条件：
 *
 * 上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。
 *
 * 本软件按“原样”提供，不提供任何形式的保证，无论是明示或暗示的，
 * 包括但不限于适销性、特定目的的适用性和非侵权性的保证。在任何情况下，
 * 无论是合同诉讼、侵权行为还是其他方面，作者或版权持有人均不对
 * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
 */

namespace Cclilshy\PRipple\Framework\Route;

use ReflectionClass;
use ReflectionException;
use ReflectionMethod;

class Route
{
    public const string GET     = 'GET';
    public const string POST    = 'POST';
    public const string PUT     = 'PUT';
    public const string DELETE  = 'DELETE';
    public const string PATCH   = 'PATCH';
    public const string HEAD    = 'HEAD';
    public const string OPTIONS = 'OPTIONS';
    public const string TRACE   = 'TRACE';
    public const string CONNECT = 'CONNECT';
    public const string STATIC  = 'STATIC';

    /**
     * @var string $method
     */
    private string           $method;
    private ReflectionMethod $methodReflection;
    private array            $methodAttributes = [];


    /**
     * @var string $class
     */
    private string          $class;
    private ReflectionClass $classReflection;
    private array           $classAttributes = [];

    /**
     * @var array $middlewares
     */
    private array $middlewares = [];

    /**
     * @var string $requestMethod
     */
    private string $requestMethod;

    /**
     * @param string $requestMethod
     * @param string $class
     * @param string $method
     * @throws ReflectionException
     */
    public function __construct(string $requestMethod, string $class, string $method)
    {
        $this->requestMethod = $requestMethod;
        $this->class         = $class;
        $this->method        = $method;

        $this->classReflection  = new ReflectionClass($class);
        $this->methodReflection = $this->classReflection->getMethod($method);
        $this->classAttributes  = $this->classReflection->getAttributes();
        $this->methodAttributes = $this->methodReflection->getAttributes();
    }

    /**
     * @return string
     */
    public function requestMethod(): string
    {
        return $this->requestMethod;
    }

    /**
     * @return string
     */
    public function getMethod(): string
    {
        return $this->method;
    }

    /**
     * @return string
     */
    public function getClass(): string
    {
        return $this->class;
    }

    /**
     * @return ReflectionMethod
     */
    public function getMethodReflection(): ReflectionMethod
    {
        return $this->methodReflection;
    }

    /**
     * @return ReflectionClass
     */
    public function getClassReflection(): ReflectionClass
    {
        return $this->classReflection;
    }

    /**
     * @return array
     */
    public function getMethodAttributes(): array
    {
        return $this->methodAttributes;
    }

    /**
     * @return array
     */
    public function getClassAttributes(): array
    {
        return $this->classAttributes;
    }

    /**
     * @return array
     */
    public function getMiddlewares(): array
    {
        return $this->middlewares;
    }

    /**
     * @param string $middleware
     * @return void
     */
    public function middleware(string $middleware): void
    {
        $this->middlewares[] = $middleware;
    }

    /**
     * @param array $middlewares
     * @return void
     */
    public function middlewares(array $middlewares): void
    {
        $this->middlewares = array_merge($this->middlewares, $middlewares);
    }
}
